/**
 * window.showModalDialog polyfill，用于不再原生支持该方法的浏览器。
 * 调用window.showModalDialog时，要将调用语句单独写在一行内，不能跨行，否则窗口关闭后的处理无法正常进行。
 * 另提供可选的第四个参数作为回调函数。如果传递回调函数，并且将窗口关闭后的处理都写在回调函数中，就不再需要将调用语句单独写在一行内。
 * window.showModalDialogPolyFill用于判断是否使用了本polyfill。
 */
(function() {
	window.getTopWindow=function(){
		var win;
		try {
			win=top.window||window;
		}catch (e) {
			win=window;
		}
		return win;
	}

	if (true) {//ie会有权限问题
		
		window.showModalDialogPolyFill = true;
		
		_getReturnValue = function(dialog, backdrop, win) {
			var returnValue = win.document.getElementById('dialog-body').contentWindow.returnValue;
			win.document.body.removeChild(dialog);
			win.document.body.removeChild(backdrop);
            return returnValue;
		}
		
	    window.showModalDialog = function(url, arg, opt, cb,win) {
	        url = url || ''; //URL of a dialog
	        arg = arg || null; //arguments to a dialog
	        opt = opt || 'dialogWidth:300px;dialogHeight:200px'; //options: dialogTop;dialogLeft;dialogWidth;dialogHeight or CSS styles
	        opt += ';position:absolute;top:30px;right:0;left:0;margin:auto;color:black;padding:1em;border:3px solid;background:white;z-index:999';
	        win=win||window;
	        var backdrop;
	        try{
	        	backdrop = win.document.body.appendChild(win.document.createElement('div'));
	        }catch(e){
	        	win=window;
	        	backdrop = win.document.body.appendChild(win.document.createElement('div'));
	        }
	        backdrop.setAttribute('style', 'position:fixed;top:0;right:0;bottom:0;left:0;background-color:rgba(0,0,0,.1)');
	        var dialog = win.document.body.appendChild(win.document.createElement('div'));
	        dialog.setAttribute('style', opt.replace(/dialog/gi, ''));
	        dialog.innerHTML = '<a href="#" id="dialog-close" style="position: absolute; top: 0; right: 4px; font-size: 20px; color: #000; text-decoration: none; outline: none;">&times;</a><iframe id="dialog-body" src="' + url + '" style="border: 0; width: 100%; height: 100%;"></iframe>';
	        win.document.getElementById('dialog-body').contentWindow.dialogArguments = arg;
	        win.document.getElementById('dialog-close').addEventListener('click', function(e) {
	            e.preventDefault();
	            dialog.close();
	        });
	        // redefine close method
	        win.document.getElementById('dialog-body').contentWindow.close = window.closeModalDialog = function() { dialog.close(); };
	        dialog.close = function () {
	        		_getReturnValue(dialog, backdrop, win);
	        	};
	        if (cb) {
	        	dialog.close = function () {
	        		cb(_getReturnValue(dialog, backdrop, win));
	        	};
	        } else {
	        	// using eval
		        var isNext = false;
		        if(showModalDialog.caller){
		        	 var caller = showModalDialog.caller.toString();
				        var nextStmts = caller.split('\n').filter(function(stmt) {
				            if(isNext || stmt.indexOf('showModalDialog(') >= 0)
				                return isNext = true;
				            return false;
				        });
				        dialog.close = function () {
				        	nextStmts[0] = nextStmts[0].replace(/(window\.)?showModalDialog\(.*\)/g, JSON.stringify(_getReturnValue(dialog,backdrop,win)));
			            	eval('{\n' + nextStmts.join('\n'));
				        };
		        }
		        throw 'Execution stopped until showModalDialog is closed';
	        }
	    };
    }
})();
